#!/usr/bin/env bash

# Copyright (c) 2025 Shenzhen Kaihong Digital Industry Development Co., Ltd.
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

srcdir=AimRT-${pkgver}

extra_args=()
if [ "${arch}" != "${host_arch}" ]; then
    extra_args+=(-DCMAKE_SYSTEM_NAME=Linux) # cross-compiling
    extra_args+=(-DCMAKE_SYSTEM_PROCESSOR=${arch})
    extra_args+=(-DCMAKE_IGNORE_PATH=/host/usr/lib)

    export CARGO_BUILD_TARGET=${triple_normalized}
fi

if [ -n "${feature_ros2}" ]; then
    extra_args+=(-DAIMRT_BUILD_WITH_ROS2=ON)
    extra_args+=(-DAIMRT_BUILD_ROS2_PLUGIN=ON)
else
    extra_args+=(-DAIMRT_BUILD_WITH_ROS2=OFF)
    extra_args+=(-DAIMRT_BUILD_ROS2_PLUGIN=OFF)
fi

if [ -n "${feature_zenoh}" ]; then
    extra_args+=(-DAIMRT_BUILD_ZENOH_PLUGIN=ON)
    extra_args+=(-Dzenohc_LOCAL_SOURCE=$(pwd)/zenoh-c-1.4.0)
else
    extra_args+=(-DAIMRT_BUILD_ZENOH_PLUGIN=OFF)
fi

if [ -n "${feature_protobuf}" ]; then
    extra_args+=(-DAIMRT_BUILD_WITH_PROTOBUF=ON)
    extra_args+=(-DAIMRT_USE_PROTOC_PYTHON_PLUGIN=ON)
    extra_args+=(-Dprotobuf_LOCAL_SOURCE=$(pwd)/protobuf-3.21.12)

    if [ "${arch}" != "${host_arch}" ]; then
        extra_args+=(-DAIMRT_USE_LOCAL_PROTOC_COMPILER=ON)
    else
        extra_args+=(-DAIMRT_USE_LOCAL_PROTOC_COMPILER=OFF)
    fi
else
    extra_args+=(-DAIMRT_BUILD_WITH_PROTOBUF=OFF)
    extra_args+=(-DAIMRT_USE_PROTOC_PYTHON_PLUGIN=OFF)
fi

extra_args+=(-Dopentelemetry_cpp_LOCAL_SOURCE=$(pwd)/opentelemetry-cpp-1.21.0)
extra_args+=(-Dopentelemetry_proto_LOCAL_SOURCE=$(pwd)/opentelemetry-proto-1.7.0)
extra_args+=(-Dmcap_LOCAL_SOURCE=$(pwd)/mcap-releases-cpp-v2.0.2)
extra_args+=(-Dzstd_LOCAL_SOURCE=$(pwd)/zstd-1.5.6)
extra_args+=(-Dasio_LOCAL_SOURCE=$(pwd)/asio-asio-1-30-2)
extra_args+=(-Dtbb_LOCAL_SOURCE=$(pwd)/oneTBB-2021.13.0)
extra_args+=(-Dpybind11_LOCAL_SOURCE=$(pwd)/pybind11-2.13.1)
extra_args+=(-Dyaml-cpp_LOCAL_SOURCE=$(pwd)/yaml-cpp-0.8.0)
extra_args+=(-Dboost_LOCAL_SOURCE=$(pwd)/boost-1.82.0)
extra_args+=(-Dlibunifex_LOCAL_SOURCE=$(pwd)/libunifex-591ec09e7d51858ad05be979d4034574215f5971)
extra_args+=(-Dfmt_LOCAL_SOURCE=$(pwd)/fmt-10.2.1)
extra_args+=(-Dgflags_LOCAL_SOURCE=$(pwd)/gflags-2.2.2)
extra_args+=(-Djsoncpp_LOCAL_SOURCE=$(pwd)/jsoncpp-1.9.6)
extra_args+=(-Dlz4_LOCAL_SOURCE=$(pwd)/lz4-1.10.0)
extra_args+=(-Dnghttp2_LOCAL_SOURCE=$(pwd)/nghttp2-1.62.1)
extra_args+=(-Dnlohmann_json_LOCAL_SOURCE=$(pwd)/json-3.11.3)
extra_args+=(-Dpaho_mqtt_c_LOCAL_SOURCE=$(pwd)/paho.mqtt.c-1.3.13)

cmake \
    -S ${srcdir} \
    -B build \
    -DCMAKE_BUILD_TYPE=Release \
    -DCMAKE_INSTALL_PREFIX=/usr \
    -DCMAKE_INSTALL_LIBDIR=lib \
    -DAIMRT_BUILD_TESTS=OFF \
    -DAIMRT_BUILD_EXAMPLES=ON \
    -DAIMRT_BUILD_DOCUMENT=ON \
    -DAIMRT_BUILD_PROTOCOLS=ON \
    -DAIMRT_BUILD_RUNTIME=ON \
    -DAIMRT_BUILD_CLI_TOOLS=ON \
    -DAIMRT_BUILD_PYTHON_RUNTIME=ON \
    -DAIMRT_USE_FMT_LIB=ON \
    -DAIMRT_BUILD_NET_PLUGIN=ON \
    -DAIMRT_BUILD_MQTT_PLUGIN=ON \
    -DAIMRT_BUILD_ICEORYX_PLUGIN=ON \
    -DAIMRT_BUILD_RECORD_PLAYBACK_PLUGIN=ON \
    -DAIMRT_BUILD_TIME_MANIPULATOR_PLUGIN=ON \
    -DAIMRT_BUILD_PARAMETER_PLUGIN=ON \
    -DAIMRT_BUILD_LOG_CONTROL_PLUGIN=ON \
    -DAIMRT_BUILD_TOPIC_LOGGER_PLUGIN=ON \
    -DAIMRT_BUILD_OPENTELEMETRY_PLUGIN=ON \
    -DAIMRT_BUILD_GRPC_PLUGIN=ON \
    -DAIMRT_BUILD_ECHO_PLUGIN=ON \
    -DAIMRT_BUILD_PROXY_PLUGIN=ON \
    -DAIMRT_BUILD_PYTHON_PACKAGE=ON \
    ${extra_args[@]} \
    || exit 1
make -C build -j ${MAX_JOBS} || exit 1
